<?php
namespace app\entries\model;
use think\Model;



/**
 *
 */
class Zhenpin extends Model
{
    // 显示珍品信息
    function showzp($page,$limit)
    {
        $teacase_ids = $this->teacase_ids();//茶类id和名称的关联数组
        $teaarea_ids = $this->teaarea_ids();//茶类id和产区的关联数组
        $bangdan_ids = $this->bangdan_ids();//榜单id和名称的关联数组

        $res = db("zhenpin")->limit($limit)->page($page)->select();
        foreach ($res as &$oneres) {

            $oneres["bangdan"] = $bangdan_ids[$oneres["bangdan_id"]];
            $oneres["teacase"] = $teacase_ids[$oneres["teacase_id"]];
            $oneres["teaarea"] = $teaarea_ids[$oneres["teaarea_id"]];
        }
        return $res;
    }

    //手动增加珍品信息
    function addzp($data)
    {
        $retval = db('zhenpin')->insert($data);
        //关联茶类和产区
        $condition = [
            "teacase_id"=>$data['teacase_id'],
            "teaarea_id"=>$data['teaarea_id'],
        ];
        $res = db('case_area')->where($condition)->find();
        if(!$res){
            db('case_area')->insert($condition);
        }
        // return 1;
        return $retval;;
    }

    //修改珍品信息
    function updatezp($id, $data)
    {
        $retval = db('zhenpin')->where('id',$id)->update($data);
        // return 1;
        return $retval;
    }

    //将Excel里的珍品信息导入数据库
    function insertExcelzp($data)
    {
        $teacase_ids = ids_teacase();//茶类名称和id的关联数组
        $teaarea_ids = ids_teaarea();//茶类产区和id的关联数组
        $bangdan_ids = ids_bangdan();//榜单名称和id的关联数组

        //Excel数据插入数据表
        foreach ($data as $onedata) {
            if(!array_key_exists($onedata["teacase"], $teacase_ids)){
                continue;
            }
            if(!array_key_exists($onedata["bangdan"], $bangdan_ids)||$bangdan_ids[$onedata['bangdan']]!=3){
                continue;
            }

            $insertdata =[];
            //判断茶产地是否在已经备案
            if(!array_key_exists($onedata["teaarea"], $teaarea_ids)){
                db("teaarea")->insert(["area"=>$onedata["teaarea"]]);
                $newId = db("teaarea")->getLastInsID();
                $teaarea_ids[$onedata["teaarea"]] = $newId;
                //关联茶类和茶产地
                $insCaseArea['teacase_id'] = $teacase_ids[$onedata["teacase"]];
                $insCaseArea['teaarea_id'] = $newId;
                db('case_area')->insert($insCaseArea);
                $teaarea_ids[$onedata["teaarea"]] = $newId;
            }

            $insertdata =[
                "teacase_id" => $teacase_ids[$onedata['teacase']],
                "teaarea_id" => $teaarea_ids[$onedata['teaarea']],
                "bangdan_id" => $bangdan_ids[$onedata['bangdan']],
                "name" => $onedata['name'],
                "producer" => $onedata['producer'],
                "zs" => $onedata['zs'],
                "gs" => $onedata['gs'],
                "ct" => $onedata['ct'],
                "ds" => $onedata['ds'],
                "other" => $onedata['other'],
                "price" => $onedata['price'],
                "contact" => $onedata['contact'],
                "phone" => $onedata['phone'],
                "wechat" => $onedata['wechat'],
                "detailintro" => $onedata['detailintro'],
                "score" => $onedata['score'],
                "addtime" => time()
            ];
            db("zhenpin")->insert($insertdata);
        }

        return 1;
    }

    //删除品牌信息
    function delZp($id)
    {
        db("zhenpin")->where("id", $id)->delete();
        return 1;
    }


    //返回某茶类对应的产地
    function reTeaArea($teacase_id)
    {
       $res = db("case_area")->field("teaarea_id")->where("teacase_id",$teacase_id)->select();
       return $res;
    }

    //返回所有品牌信息
    function getAllzp()
    {
        $res = db("zhenpin")->order('score desc')->select();
        return $res;
    }

    function getOther()
    {
        $res = db("zhenpin")->where('bangdan_id', 4)->order('score desc')->select();
        return $res;
    }





}















?>